package cn.iocoder.yudao.module.system.controller.admin.datascope;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.datascope.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.datascope.DataScopeDO;
import cn.iocoder.yudao.module.system.service.datascope.DataScopeService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 权限范围")
@RestController
@RequestMapping("/system/data-scope")
@Validated
public class DataScopeController {

    @Resource
    private DataScopeService dataScopeService;

    @PostMapping("/create")
    @Operation(summary = "创建权限范围")
    @PreAuthorize("@ss.hasPermission('system:data-scope:create')")
    public CommonResult<Long> createDataScope(@Valid @RequestBody DataScopeSaveReqVO createReqVO) {
        return success(dataScopeService.createDataScope(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新权限范围")
    @PreAuthorize("@ss.hasPermission('system:data-scope:update')")
    public CommonResult<Boolean> updateDataScope(@Valid @RequestBody DataScopeSaveReqVO updateReqVO) {
        dataScopeService.updateDataScope(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除权限范围")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:data-scope:delete')")
    public CommonResult<Boolean> deleteDataScope(@RequestParam("id") Long id) {
        dataScopeService.deleteDataScope(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得权限范围")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:data-scope:query')")
    public CommonResult<DataScopeRespVO> getDataScope(@RequestParam("id") Long id) {
        DataScopeDO dataScope = dataScopeService.getDataScope(id);
        return success(BeanUtils.toBean(dataScope, DataScopeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得权限范围分页")
    @PreAuthorize("@ss.hasPermission('system:data-scope:query')")
    public CommonResult<PageResult<DataScopeRespVO>> getDataScopePage(@Valid DataScopePageReqVO pageReqVO) {
        PageResult<DataScopeDO> pageResult = dataScopeService.getDataScopePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataScopeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出权限范围 Excel")
    @PreAuthorize("@ss.hasPermission('system:data-scope:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataScopeExcel(@Valid DataScopePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataScopeDO> list = dataScopeService.getDataScopePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "权限范围.xls", "数据", DataScopeRespVO.class,
                        BeanUtils.toBean(list, DataScopeRespVO.class));
    }

}